<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-02-05 22:30:03
 * Desc:
 */

namespace app\admin\controller\system;

use app\admin\controller\adminAuthController;
use app\base\model\system\SystemConfig;
use app\core\exception\ErrorCode;

class SystemConfigController extends adminAuthController
{
    public function getConfigList()
    {
        $name = input('name/s', '');
        $where = [];

        if ($name) {
            $where['name'] = ['like', "%{$name}%"];
        }

        $type = input('type/d', 0);

        if ($type) {
            $where['type'] = $type;
        }

        $size = input('pageSize/d', 20);

        $data = SystemConfig::getSystemConfigList($where, $size);
        return $this->successReturn($data);
    }

    public function addConfig()
    {
        $name = input('name/s', '');
        if (empty($name)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'name为空');
        }

        $value = input('value/s', '');
        if (empty($value)) {
            //return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'value为空');
        }

        $type = input('type/d', 0);
        if (empty($type)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'type为空');
        }

        $description = input('description/s', '');
        if (empty($description)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'description为空');
        }

        if (SystemConfig::getSystemConfigByName($name)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'name已存在');
        }

        $create_time = date('Y-m-d H:i:s');

        $model = new SystemConfig(compact('name', 'value', 'type', 'description', 'create_time'));
        $model->save();

        SystemConfig::delCache($name);

        return $this->successReturn();
    }

    public function updateConfig()
    {
        $name = input('name/s', '');
        if (empty($name)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'name为空');
        }

        $value = input('value/s', '');
        if (empty($value)) {
            //return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'value为空');
        }

        $type = input('type/d', 0);
        if (empty($type)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'type为空');
        }

        $description = input('description/s', '');
        if (empty($description)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'description为空');
        }

        $id = input('id/d', 0);
        if (empty($id)) {
            return $this->errorReturn(ErrorCode::COMMON_LACK_PARAMS, 'id为空');
        }


        $model = SystemConfig::get($id);

        if ($model == null) {
            return $this->errorReturn(ErrorCode::DATA_NOT_FOUND);
        }

        if ($model->name !== $name) {
            //return $this->errorReturn(ErrorCode::COMMON_ERROR, '名称不能修改');
        }

        $update_time = date('Y-m-d H:i:s');

        $model->save(compact('name', 'value', 'type', 'description', 'update_time'));

        SystemConfig::delCache($name);

        return $this->successReturn();
    }


}